package com.ibeeking.found.uaa.b.config.filter;

import cn.hutool.http.HttpStatus;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import com.alibaba.nacos.common.http.param.MediaType;
import org.springframework.http.HttpHeaders;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

/**
 * @ClassName AuthenticationEntryPoint
 * @Description
 * @Author ibeeking
 * @Date 2020-12-10 16:33
 **/
@Component
public class ClientExceptionEntryPoint {

    public AuthenticationEntryPoint clientExceptionEntryPoint() {
        return (request, response, e) -> {
            response.setStatus(HttpStatus.HTTP_UNAUTHORIZED);
            response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
            response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
            response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache");
            response.getWriter().print(JsonUtils.toJsonStr(ResponseResult.fail(HttpStatus.HTTP_UNAUTHORIZED, e.getMessage())));
            response.getWriter().flush();
        };
    }

}
